package 寒假刷题;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class 二叉搜索树的所有元素 {

    public class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
  }
    ArrayList<Integer> list = new ArrayList<>();
    public List<Integer> getAllElements(TreeNode root1, TreeNode root2) {
         dfs(root1);
         dfs(root2);
        Collections.sort(list);
         return list;
    }
    public void dfs(TreeNode root){
        if (root==null)
            return;
        dfs(root.left);
        list.add(root.val);
        dfs(root.right);
    }
}
